package com.java.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.PageList;
import com.java.utils.ResponseUtil;;

@RestController
@RequestMapping("/buy")
public class BuyController {
	private BuyService buyService;

	public BuyService getBuyService() {
		return buyService;
	}
	@Autowired
	private ThingTypeService thingtypeService;
	@Autowired
	private BuyListService buylistService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private ThingType2Service thingtype2Service;
	@Autowired
	public void setBuyService(BuyService buyService) {
		this.buyService = buyService;
	}
	@Autowired
	private ThingService thingService;
	@Autowired
	private BuyingService buyingService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;



	@CrossOrigin(origins = "*")
	@RequestMapping("/add")
	@ResponseBody
	public JsonBody add(@RequestBody Buying b) {
		if(buyingService.GetCount(b)==0)
			return Util.SetMap("您的购物车中无任何商品",false);
		Buy bl=new Buy();

		List<Buying> buying=buyingService.Get(b);
		Buy buy=new Buy();
		buy.setClientId(b.getClientId());

		int id = buyService.Add(buy);
		id=buy.getId();

		if(id<1)
			return Util.SetMap("提交失败",false);
		try {
			int count=0;
			for(Buying bi:buying)
			{
				BuyList buylist=new BuyList();
				buylist.setBuyId(id);
				buylist.setThingId(bi.getThingId());
				buylist.setNum(bi.getNum());
				int success=buylistService.Add(buylist);
				count+=success;
				if(success>0)
					buyingService.Del(bi.getId());
			}
			if(count==buying.size())
				return Util.SetMap("下单成功", true);
			else
				return Util.SetMap("提交失败", false);
		} catch (Exception e) {
			return Util.SetMap(e.getMessage(), false);
		}
	}

	@CrossOrigin(origins = "*")
	@RequestMapping("/list")
	@ResponseBody
	public JsonBody list(@RequestBody Buy b) {
		List<Buy> bs= buyService.Get(b);
		for(Buy buy:bs)
		{
			buy.setClient(clientService.GetByID(buy.getClientId()));
		}
		return Util.SetData(bs);
	}

	@CrossOrigin(origins = "*")
	@RequestMapping("/del")
	@ResponseBody
	public JsonBody Del(int id) {
		Buy b=new Buy();
		b.setId(id);
		Buy bl=buyService.GetByID(b);
		if(bl.getState()>0)
			return Util.SetError("该订单已发出，不能取消");
		int r = buyService.Del(id);

		if(r>0)
			return Util.SetMap("退单成功！", false);
		else
			return Util.SetMap("退单失败！", false);
	}

	@CrossOrigin(origins = "*")
	@RequestMapping("/state")
	@ResponseBody
	public JsonBody state(@RequestBody Buy bl) {
		int r = buyService.EditState(bl);

		if(r>0)
			return Util.SetMap("操作成功！", true);
		else
			return Util.SetMap("操作失败！", false);
	}


}
